package com.zhxy.manager.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhxy.manager.dao.LibraryDao;
import com.zhxy.manager.entity.Library;
import com.zhxy.manager.service.intf.LibraryService;
import com.zhxy.manager.vo.PageBean;
import com.zhxy.manager.vo.R;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
public class LibraryServiceImpl implements LibraryService {
    @Autowired
    private LibraryDao dao;

 @Override
    public R save(Library library) {
        if (dao.add(library)>0){
            return R.ok();
        }else {
            return R.fail();
        }
    }

@Override
    public PageBean queryPage(int page, int limit) {
        //1.分页
        PageHelper.startPage(page, limit);
        //2.获取分页结果
        PageInfo<Library> pageInfo = new PageInfo<>(dao.selectAll());
        //3.返回分页的数据(1.获取数据库的总条数，2，返回分页数据)
        return  PageBean.bean(pageInfo.getTotal(),pageInfo.getList());
    }

    @Override
    public R del(int id) {
       if(id>0){
           if (dao.del(id)>0){
               return R.ok();
           }
       }
        return  R.fail();
    }

    @Override
    public R update(Library library) {
      if(dao.update(library)>0){
          return R.ok();
      }else {
          return R.fail();
      }

    }

    @Override
    public PageBean queryPageWhere(String bookname, int page, int limit) {
        //1.验证 查询条件 如果为 "" 那么赋值为null
        if(!StringUtils.hasLength(bookname)){
            bookname=null;
        }else{
            bookname="%"+bookname+"%";//包含  模糊查询
        }
        //2.分页
        PageHelper.startPage(page, limit);
        //3.获取分页结果
        PageInfo<Library> pageInfo=new PageInfo<>(dao.selectWhere(bookname));
        //4.返回分页的数据
        return PageBean.bean(pageInfo.getTotal(), pageInfo.getList());

    }


    @Override
    public R delBatch(int[] ids) {
        //1.验证参数
        if(ids!=null && ids.length>0){
            //2.执行
            if(dao.delBatch(ids)>0){
                return R.ok();
            }
        }
        return R.fail();
    }

}
